import 'package:flutter/material.dart';
import 'package:net/core/hi_base_tab_state.dart';
import 'package:net/http/dao/home_dao.dart';
import 'package:net/model/home_mo.dart';
import 'package:net/model/video_model.dart';
import 'package:net/widget/h_banner.dart';
import 'package:net/widget/video_card.dart';
import 'package:flutter_nested/flutter_nested.dart';

class HomeTabPage extends StatefulWidget {
  final String categoryName;
  final List<BannerMo> bannerList;
  HomeTabPage({Key key, this.categoryName, this.bannerList}) : super(key: key);

  @override
  _HomeTabPageState createState() => _HomeTabPageState();
}

class _HomeTabPageState extends HBaseTabState<HomeMo, VideoModel, HomeTabPage> {
  @override
  void initState() {
    super.initState();
  }

  _banner() {
    return Padding(
      padding: EdgeInsets.only(left: 5, right: 5),
      child: HBanner(
        bannerList: widget.bannerList,
        bannerHeight: 180,
        padding: EdgeInsets.all(10),
      ),
    );
  }

  //性能优化得知 StaggeredGridView.countBuilder这个插件厉害是厉害 但是还有点卡顿-所有
  // 用老师的一个插件
  // @override
  // get contentChild => StaggeredGridView.countBuilder(
  //     controller: scrollController,
  //     itemCount: dataList.length,
  //     physics:
  //         const AlwaysScrollableScrollPhysics(), //告诉列表即使没有内容充满整个屏幕也可以滚动（这样即使没有充满也可以下拉刷新，但是上拉是否需要就需要自己根据dis那个距离做临界值判断）
  //     padding: EdgeInsets.only(top: 10, left: 10, right: 10),
  //     crossAxisCount: 2,
  //     itemBuilder: (BuildContext context, int index) {
  //       //有banner时第一个item位置显示banner
  //       if (widget.bannerList != null && index == 0) {
  //         return Padding(padding: EdgeInsets.only(bottom: 8), child: _banner());
  //       } else {
  //         return VideoCard(videoMo: dataList[index]);
  //       }
  //     },
  //     staggeredTileBuilder: (int index) {
  //       // index告诉我们哪一行
  //       if (widget.bannerList != null && index == 0) {
  //         //  StaggeredTile.fit(2)占几列 总的列数由上面crossAxisCount决定
  //         return StaggeredTile.fit(2);
  //       } else {
  //         return StaggeredTile.fit(1);
  //       }
  //     });
  @override
  get contentChild => HiNestedScrollView(
      controller: scrollController,
      padding: EdgeInsets.only(top: 10, left: 10, right: 10),
      headers: [
        if (widget.bannerList != null)
          Padding(padding: EdgeInsets.only(bottom: 8), child: _banner())
        else
          Container()
      ],
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 1,
      ),
      itemCount: dataList.length,
      itemBuilder: (BuildContext context, int index) {
        return VideoCard(videoMo: dataList[index]);
      });

  @override
  Future<HomeMo> getData(int pageIndex) async {
    HomeMo result = await HomeDao.get(widget.categoryName,
        pageIndex: pageIndex, pageSize: 10);
    return result;
  }

  @override
  List<VideoModel> parseList(HomeMo resulet) {
    return resulet.videoList;
  }

  @override
  bool get wantKeepAlive => true;
}
